package netty.test;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.util.concurrent.GenericFutureListener;

public class Client {

	public static void main(String[] args) throws Exception {
		
		EventLoopGroup workGroup = new NioEventLoopGroup();
		Bootstrap b = new Bootstrap();
		b.group(workGroup)
		.channel(NioSocketChannel.class)
		.handler(new ChannelInitializer<SocketChannel>() {
			@Override
			protected void initChannel(SocketChannel sc) throws Exception {
				sc.pipeline().addLast(new ClientHandler());
			}
		});
		
		ChannelFuture cf1 = b.connect("127.0.0.1", 8765).sync();
		
		//buf
		//cf1.channel().writeAndFlush(Unpooled.copiedBuffer("777".getBytes()));
		//写完关闭
		cf1.channel().writeAndFlush(Unpooled.copiedBuffer("777".getBytes())).addListener(ChannelFutureListener.CLOSE);

		//阻塞程序，不让程序结束   相当于调用sleep
		cf1.channel().closeFuture().sync();
		workGroup.shutdownGracefully();
		
	}
}
